export default {
  namespace: 'tabs', // 模型命名空间，用于标识状态
  state: {
    list: [], // 标签页列表，每个元素含 `key`（路由）、`title`（标签标题）
    activeKey: '', // 当前激活的标签 key（对应路由）
  },
  reducers: {
    // 添加标签页（若已存在则仅切换激活态）
    addTab(state: { list: any[]; }, { payload }: any) {
      const isExist = state.list.some(tab => tab.key === payload.key);
      if (!isExist) {
        return {
          ...state,
          list: [...state.list, payload],
          activeKey: payload.key,
        };
      }
      return { ...state, activeKey: payload.key };
    },
    // 移除标签页
    removeTab(state: { list: any[]; activeKey: any; }, { payload }: any) {
      const newList = state.list.filter(tab => tab.key !== payload.key);
      return {
        ...state,
        list: newList,
        // 若删除的是“当前激活标签”，则切换到列表首个标签（或空）
        activeKey: state.activeKey === payload.key 
          ? (newList[0]?.key || '') 
          : state.activeKey,
      };
    },
    // 切换激活标签
    setActiveKey(state: any, { payload }: any) {
      return { ...state, activeKey: payload.key };
    },
  },
};